#include "DownloadPointListCmd.h"

DownloadPointListCmd::DownloadPointListCmd(QObject *parent) :
    BaseCmd(parent)
{
    this->m_protocolType = ProtocolType::CloudProtocol;
    this->m_functionCode = FunctionCode;
}

Package DownloadPointListCmd::toPackage()
{
    Package package;
    QByteArray body;

    quint16 size = m_downloadPointList.size();

    if(size != 0)
    {
        body.append(DataTypeConver::intToByteArray(size,2));

        for(int i=0; i<size; i++)
        {
            ControlPointStruct downloadPoint = m_downloadPointList.at(i);
            body.append(DataTypeConver::intToByteArray(downloadPoint.address(),2));
            body.append(DataTypeConver::intToByteArray(downloadPoint.value(),2));
        }
    }

  //  body = encrypt(body,KeyType::AES);

    package.setId(m_cmdId);
    package.setFunction(FunctionCode);
    package.setBody(body);

    return package;
}

void DownloadPointListCmd::unPackage(Package package)
{
    m_cmdId = package.id();
    quint16 index = 0;
    QByteArray body;

  //  body = decrypt(package.body(),KeyType::AES);

    quint16 size = body.mid(index,2).toHex().toInt(0,16);
    index += 2;

    for(int i=0; i<size; i++)
    {
        ControlPointStruct downloadPoint;
        int address = body.mid(index,2).toHex().toInt(0,16);
        index += 2;
        int value = body.mid(index,2).toHex().toInt(0,16);
        index += 2;

        downloadPoint.setAddress(address);
        downloadPoint.setValue(value);
        m_downloadPointList.append(downloadPoint);
    }
}

QList<ControlPointStruct> DownloadPointListCmd::downloadPointList() const
{
    return m_downloadPointList;
}

void DownloadPointListCmd::setDownloadPointList(const QList<ControlPointStruct> &downloadPointList)
{
    m_downloadPointList = downloadPointList;
}

